package com.atguigu.gmall.product.service.impl;



import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**

 *
 * @author ZYB
 * @since 2021-05-11
 */
@Service
public class ManagerServiceImpl  implements ManagerService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;
    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;
    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    @Autowired
    private BaseAttrInfoMapper BaseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private SpuInfoMappeer spuInfoMappeer;

    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;

    @Autowired
    private SpuImageMapper spuImageMapper;

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Autowired
    private SkuInfoMapper skuInfoMapper;

    @Autowired
    private SkuImageMapper skuImageMapper;

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    @Autowired
    private SkuSaleAttrValueMapper skuSaleAttrValueMapper;



    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {
//        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("category1_id",category1Id);
//        return baseCategory2Mapper.selectList(queryWrapper);
        return baseCategory2Mapper.selectList(new QueryWrapper<BaseCategory2>().
                eq("category1_id",category1Id) );
    }

    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
//        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("category1_id",category1Id);
//        return baseCategory2Mapper.selectList(queryWrapper);
        return baseCategory3Mapper.selectList(new QueryWrapper<BaseCategory3>().
                eq("category2_id",category2Id) );
    }

    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        return BaseAttrInfoMapper.attrInfoList(category1Id,category2Id,category3Id);
    }

    //添加平台属性及值
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {

        //1:平台属性表
        BaseAttrInfoMapper.insert(baseAttrInfo);

        //2:平台属性值表
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        attrValueList.forEach(baseAttrValue -> {
            //设置外键
            baseAttrValue.setAttrId(baseAttrInfo.getId());
            baseAttrValueMapper.insert(baseAttrValue);
        });

    }

    @Override
    public IPage baseTrademark(Integer page, Integer limit) {

        return baseTrademarkMapper.selectPage(new Page(page,limit),null);
    }

    @Override
    public IPage spuInfoList(Integer page, Integer limit, Long category3Id) {
        return spuInfoMappeer.selectPage(new Page(page,limit),new QueryWrapper<SpuInfo>().eq("category3_id",category3Id));
    }

    @Override
    public List<BaseTrademark> getTrademarkList() {
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public List<BaseSaleAttr> baseSaleAttrList() {
        return baseSaleAttrMapper.selectList(null);
    }

    @Override//保存商品表涉及四张表操作
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1.商品表 直接保存 主键自动生成
        spuInfoMappeer.insert(spuInfo);
            //图片表 外
        //获取商品表中的图片集合
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        //循环保存
        spuImageList.forEach(spuImage -> {
            //需要单独设置外键id
            spuImage.setSpuId(spuInfo.getId());
            spuImageMapper.insert(spuImage);
        });
        //销售属性表
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        spuSaleAttrList.forEach(spuSaleAttr -> {
            spuSaleAttr.setSpuId(spuInfo.getId());
            spuSaleAttrMapper.insert(spuSaleAttr);

            //销售属性值表
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            spuSaleAttrValueList.forEach(spuSaleAttrValue -> {
                //商品id赋值
                spuSaleAttrValue.setSpuId(spuInfo.getId());
                //属性名称赋值
                spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                spuSaleAttrValueMapper.insert(spuSaleAttrValue);
            });
        });
    }

    @Override
    public List<SpuImage> spuImageList(Long spuId) {
        return spuImageMapper.selectList(new QueryWrapper<SpuImage>().eq("spu_id",spuId));
    }

    @Override
    public List<SpuSaleAttr> spuSaleAttrList(Long spuId) {
        return spuSaleAttrMapper.spuSaleAttrList(spuId);
    }

    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        //1.库存表
        skuInfoMapper.insert(skuInfo);
        //2.库存的图片表
        skuInfo.getSkuImageList().forEach(skuImage -> {
            //外键
            skuImage.setSkuId(skuInfo.getId());
            //保存库存图片表
            skuImageMapper.insert(skuImage);
        });
        //3.库存与平台属性及值的中间表
        skuInfo.getSkuAttrValueList().forEach(skuAttrValue -> {
            skuAttrValue.setSkuId(skuInfo.getId());
            skuAttrValueMapper.insert(skuAttrValue);

        });
        //4.库存与销售属性及值的中间表
        skuInfo.getSkuSaleAttrValueList().forEach(skuSaleAttrValue -> {
            //库存ID 外键
            skuSaleAttrValue.setSkuId(skuInfo.getId());
            //商品Id 外键
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());

            skuSaleAttrValueMapper.insert(skuSaleAttrValue);
        });

    }

    @Override
    public IPage skuListPage(Integer page, Integer limit) {

        return skuInfoMapper.selectPage(new Page(page,limit),null);
    }

    @Override
    public void onSale(Long skuId) {
        //1:修改DB中库存表 is_sale
        //  不走1.1 先查询  select * from sku_info where id = ?  连接mysql
        //  直接走1.2 再修改 update sku_info set is_sale = 1 where id = ? 连接mysql
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.ISSALE);
        skuInfoMapper.updateById(skuInfo);
        //TODO 2：发消息 更新ES索引

    }

    @Override
    public void cancelSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.CANCELSALE);
        skuInfoMapper.updateById(skuInfo);
        //TODO 2：发消息 删除索引库

    }
}
